// 草-静态分析
(function(win) {
    const urlKeyMap = [
        { prop: 'name', label: 'URL', copy: true },
        { prop: 'md5', label: 'MD5', copy: true },
        { prop: 'sha1', label: 'SHA1', copy: true },
        { prop: 'sha256', label: 'SHA256', copy: true }
    ]
    const  fileKeyMap = [
        { prop: 'name', label: '文件名称', copy: true },
        { prop: 'size', label: '文件大小' },
        { prop: 'qex_type', label: '文件类型' },
        { prop: 'type', label: '文件格式' },
        { prop: 'md5', label: 'MD5', copy: true },
        { prop: 'sha1', label: 'SHA1', copy: true },
        { prop: 'sha256', label: 'SHA256', copy: true },
        { prop: 'crc32', label: 'CRC32' },
        { prop: 'ssdeep', label: 'SSDeep' }
    ]
    const peinfoKeyMap = [
        { prop: 'pe_timestamp', label: '编译时间' },
        { prop: 'pe_imphash', label: '导入表哈希' },
        { prop: 'pdb_path', label: '符号路径' },
        { prop: 'pe_shells', label: '壳', format: (value) => value.join(',') }
    ]
    function handleTableData(keyMap, list, keyWidth = '100px') {
        const columnData = keyMap.map((item) => {
            return {
                prop: item.prop,
                label: item.label || item.prop,
                copy: item.copy,
                format: item.format ? item.format : undefined
            }
        })
        const tableData = list
        return { columnData, tableData, keyWidth }
    }

    var taskInfo = result.info
    var staticinfo = null // 静态信息

    const initJtfx = () => {
        $.get(ctx + 'serve/sandboxReportStatic/' + taskId, function(res) {
            console.info('res-sandboxReportStatic:', res)
            staticinfo = res.data
            staticinfo.target = result.target.file
            staticinfo.taskOS = taskInfo.machine.os
            initWjnr() // 文件内容

            initPeInfo() //  PE 信息
        })
    }
    win.initJtfx = initJtfx

    // 文件内容
    function initWjnr() {
        const getTarget = () => {
            return { ...staticinfo.target, size: `${staticinfo.target.size} 字节` }
        }
        const target = getTarget()


        const getInfoData = () => {
            const map = taskInfo.category === 'url' ? urlKeyMap : fileKeyMap
            return handleTableData(map, {
                ...target,
                name: taskInfo ? taskInfo.filename || taskInfo.url : target.file.name
            })
        }

        const infoData = getInfoData()

        if(taskInfo.category === 'url') {
            $('#staticName').html('URL信息')
        } else{
            $('#staticName').html('文件信息')
        }

        const infoHtml = SandJsonTable(infoData, true)
        $('#staticInfo').html(infoHtml)
    }

    // PE信息
    function initPeInfo() {
        const peinfo = staticinfo.static.peinfo

        const peData = handleTableData(peinfoKeyMap, peinfo)
        const peHtml = SandJsonTable(peData, true)
        $('#staticPe').html(peHtml)
    }


    function SandJsonTable(data, noborder) {
        const columnData = data.columnData || []
        const tableData = data.tableData || []
        const keyWidth = data.keyWidth || '120px'

        const htmls = []
        htmls.push('<div class="sand-jsonTable">')

        columnData.forEach(item => {
            const styleKey = `width:${keyWidth};`
            const styleVal = `width:calc( 100% - ${keyWidth} - 3px );`
            htmls.push('<div class="sand-jsonTable__item">')
            htmls.push('  <div class="sand-jsonTable__item--key" style="' + styleKey + '">' + item.label + '</div>')
            htmls.push('  <div class="sand-jsonTable__item--value" style="' + styleVal + '">')
            htmls.push(item.format ? item.format(tableData[item.prop]) : tableData[item.prop] || '-')
            htmls.push('  </div>')
            htmls.push('</div>')
        })

        htmls.push('</div>')
        return htmls.join('')

    }


})(window)
